package com.problem.basic;
import com.util.solution.inter.Solution;

/**
 * a help class, which is used for the implementation of M2M
 * @author heaven
 * @param <T>
 *
 */
public abstract class M2MHelper<T> extends Problem {
	/**
	 * find the index 
	 * @param s
	 * @param pop_size
	 * @param weights
	 * @return
	 */
	public int getIndex(Solution s, int pop_size, double [] weights) {
		double x1 = 1.0*s.object_val[0]/(s.object_val[0]+s.object_val[1]);
		double x2 = 1.0*s.object_val[1]/(s.object_val[0]+s.object_val[1]);
		double max = Double.MIN_VALUE;
		int index = -1;
		for(int i=0;i<pop_size;i++) {
			double y1 = weights[2*i];
			double y2 = weights[2*i+1];
			double cosVal = (x1*y1+x2*y2)/(Math.sqrt(x1*x1+x2*x2)*Math.sqrt(y1*y1+y2*y2));
			if(cosVal>max) { 
				max = cosVal; 
				index = i;
			}
		}
		return index;
	}
}
